<template>
    <div class="app-update">
      <pre v-highlightjs class="code-pre">
      <code class="javascript">
        /**
         *结合HBuilder检测app更新代码
         */
        let os = navigator.userAgent;
        document.addEventListener('plusready', () => {
          plus.runtime.getProperty(plus.runtime.appid, inf => {
            let ver = inf.version;
            if (/iPhone/.test(os)) {
              this.$http.get(`https://itunes.apple.com/cn/lookup?id=**********`)  //*部分为app的id
                .then(({data}) => {
                  let url = data.results[0].trackViewUrl;
                  let version = data.results[0].version;
                  if (version > ver) {
                    MessageBox({
                      title: '更新提示',
                      message: '有新版本可以更新！',
                      showCancelButton: true,
                      showConfirmButton: true,
                      confirmButtonText: '前往更新',
                      cancelButtonText: '取消'
                    }).then(res => {
                      if (res === 'confirm') {
                        window.open(url)
                      }
                    })
                  }
                })
            }
            if (/Android/.test(os)) {
              this.$http.get(`/ad/edition`)
                .then(({data}) => {
                  this.downUrl = data.info.ad_down;
                  if (ver < data.info.ad_edition) {
                    MessageBox({
                      title: '更新提示',
                      message: '有新版本可以更新！',
                      showCancelButton: true,
                      showConfirmButton: true,
                      confirmButtonText: '前往更新',
                      cancelButtonText: '取消'
                    }).then(res => {
                      if (res === 'confirm') {
                        window.open('market://details?id=******')  //*部分表示app包名
                      }
                    })
                  }
                })
            }
          });
        });
      </code>
    </pre>
    </div>
</template>

<script>
    export default {
        name: "appUpdate"
    }
</script>

<style scoped>

</style>
